package com.cloudptt.wp.ui.address.map;

import java.util.ArrayList;
import java.util.List;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.address.adapter.VideoAdapter;
import com.mcptt.config.model.McpttLoginInfo;
import com.mcptt.db.model.UserInfo;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MapCallUtil {
	
	private Context context;
		
	private ListView mListView;
	
	private List<UserInfo> list = new ArrayList<UserInfo>();
	
	private McpttLoginInfo mcpttLoginInfo;
	
	private VideoAdapter adapter;
	
	public MapCallUtil(Context ctx) {
		// TODO Auto-generated constructor stub
		context = ctx;
	}
	
	public void showListView(boolean isShow){
		if(mListView != null){
			mListView.setVisibility(isShow ? View.VISIBLE : View.GONE);
		}
	}
	
	public void initVideoCallLv(ListView lv){
		mListView = lv;
		adapter = new VideoAdapter(context, list);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				doClickCallBtnAction(list.get(position), true);
			}
		});

	}
	
	private void doClickCallBtnAction(UserInfo userInfo, boolean useVideo ){
		String num = userInfo.getAddrId();
		String curMcId = mcpttLoginInfo.getMcIdInfo().getAddrId();
		if(num.equals(curMcId)){
			CommDlg.show(context, context.getResources().getString(R.string.call_self));
			return;
		}

//		if( useVideo ){
//			Camera camera = null;
//			try{
//				if( null == (camera=Camera.open() ) ){
//					CommDlg.showToast( context, context.getString( R.string.confirm_dev_camera ) );
//					Log.e("VideoAdapter", "OpenCamera error");
//					return;
//				}
//			}catch(Exception ex){
//				CommDlg.showToast( context, context.getString( R.string.confirm_dev_camera ) );
//				Log.e("VideoAdapter", "OpenCamera error:" + ex.getMessage( ) );
//				return;
//			}
//			if(null!= camera){
//				camera.release( );
//			}
//		}
		SysApplication app = ((SysApplication) context.getApplicationContext());
		Product product = app.getProduct();
		product.getPCallEventAdapter().makePrivateCall(userInfo, CallAttr.CallType_Private,false,false,"");
//		CallFunction.makePrivateCall(context,userInfo,CallAttr.CallType_Webrtc_Single, useVideo);
	}
	
	public void updateData(List<UserInfo> list){
		adapter.setData(list);
	}
}
